//
//  StudyCenterVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/16.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "StudyCenterVC.h"
#import "studyCollectionCell.h"
#import "tuiJianCollectionCell.h"
#import "searchCollectionCell.h"
#import "RecommendClassVC.h"
#import "TuiJianZuoPinVC.h"
#import "BaseCollectionCell.h"
#import "SearchVC.h"
#import "PracticeVC.h"
#import "StudyInforListVC.h"

@interface StudyCenterVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UIButton *back_btn;
@property (strong, nonatomic) IBOutlet UILabel *title_lab;
@property (strong, nonatomic) IBOutlet UIButton *class_btn;
@property (strong, nonatomic) IBOutlet UIButton *art_btn;
@property (strong, nonatomic) IBOutlet UIButton *search_btn;

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end

@implementation StudyCenterVC{
    RequestServices *service;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学习中心";
    //导航右按钮 搜索
    [self createNavigationBarRightBarButtonItemWithTitle:@"" font:1 image:@"search" target:self action:@selector(pushSearch)];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"tuiJianCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"tuiJianCollectionCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"studyCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"studyCollectionCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"searchCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"searchCollectionCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"BaseCollectionCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BaseCollectionCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;//隐藏竖滚动条
    self.collectionView.pagingEnabled = NO;
    
    list_arr = [[NSMutableArray alloc]init];
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    __weak __typeof(self)weakSelf = self;
    [self headerRefresh];
    self.collectionView.mj_header = [baseRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf headerRefresh];
    }];
    self.collectionView.mj_footer = [baseRefreshFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    self.collectionView.mj_footer.hidden = YES;
}

- (void)headerRefresh
{
    page_index = 1;
    [service getClassList:[NSString stringWithFormat:@"%ld",page_index] tag:8000];
}

- (void)footerRefresh
{
    page_index++;
    [service getClassList:[NSString stringWithFormat:@"%ld",page_index] tag:8001];
}


#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
    
    [self HideerrorView];
    [self HideNoDataView];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
    if (tag == 8000) {
        if ([tempArr CheckNSArrayData]) {
            list_arr = [StudyListModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
            
        }
        if (tempArr.count == 0) {
            [self ShowNoDataView:self.collectionView Orgin:0];
        }
    }else {
        [list_arr addObjectsFromArray:[StudyListModel mj_objectArrayWithKeyValuesArray:tempArr]];
    }
    
    [self.collectionView reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
    [self ShowNewMsg:error.domain];
    if (tag == 8000) {
        [self ShowerrorView:self.collectionView Orgin:0];
    }
}

#pragma mark ----- collectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else if (section == 1){
        return list_arr.count;
    }
    return 3;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
         return CGSizeMake((collectionView.frame.size.width - 80)/4, (collectionView.frame.size.height)/4);
    }else if (indexPath.section == 1){
        return CGSizeMake((collectionView.frame.size.width - 80)/6, (collectionView.frame.size.height)/4);
    }else{
        return CGSizeMake((collectionView.frame.size.width - 80)/3, (collectionView.frame.size.height)*0.7);
    }
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        tuiJianCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tuiJianCollectionCell" forIndexPath:indexPath];
//        cell.model = list_arr[indexPath.item];
        return cell;
    }else if (indexPath.section == 1){
        studyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"studyCollectionCell" forIndexPath:indexPath];
        cell.model = list_arr[indexPath.item];
        return cell;
        
    }else{
        searchCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"searchCollectionCell" forIndexPath:indexPath];
//        cell.model = list_arr[indexPath.item];
        return cell;
    }
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

//每个section的header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    BaseCollectionCell *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BaseCollectionCell" forIndexPath:indexPath];
    view.base_lab.font = AutoLabFont2;
    if (indexPath.section == 0) {
         view.base_lab.text = @"推荐课程";
    }
    if (indexPath.section == 2) {
        view.base_lab.text = @"推荐作品";
    }
    return view;
}

//header的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section != 1) {
        return CGSizeMake(collectionView.frame.size.width, 50);
    }else{
        return CGSizeMake(0,0);
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        StudyInforListVC *vc = [[StudyInforListVC alloc]init];
        StudyListModel *model = list_arr[indexPath.row];
        vc.id = model.id;
        [self PushToViewController:vc];
    }
}

//推荐课程
- (IBAction)tuiJian_class:(id)sender {
    RecommendClassVC *vc = [[RecommendClassVC alloc]init];
    [self PushToViewController:vc];
}


//推荐作品
- (IBAction)tuiJian_art:(id)sender {
    TuiJianZuoPinVC *vc = [[TuiJianZuoPinVC alloc]init];
    [self PushToViewController:vc];
}

//搜索
-(void)pushSearch{
//    SearchVC *vc = [[SearchVC alloc]init];
    PracticeVC *vc = [[PracticeVC alloc]init];
    [self PushToViewController:vc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
